import Vue from 'vue'
import Vuex,{createLogger} from 'vuex'
import axios from "axios"
Vue.use(Vuex)

export default new Vuex.Store({
  plugins:[new createLogger()],
  state: {
    arr:[]
  },
  getters: {
  },
  mutations: {
    setlist(state,{payload}) { // z在仓库里通过commit触发的要解构
       state.arr = payload
    },
    addfn (state,payload) { // 组件往仓库传的时候如果不是通过commit触发payload不需要结构
    
      console.log(payload,"palodad");
      state.arr.unshift(payload)
    },
    delfn (state,payload) { // 删除数据的方法
       console.log(payload)
       state.arr = state.arr.filter(item=>item !== payload)
        // state.arr.splice(payload,1)
    },
    editfn (state,payload) {
        console.log(payload,"payload");
       state.arr.splice(payload.index,1,payload)
    },
    searchfn (state,payload) {
        state.arr = state.arr.filter((item,index)=>{
          return item.name === payload
        })
    }
   },
  actions: {
    getlist({commit,state}) {
        axios.get("/api/list").then(res=>{
            commit({
              type:"setlist",
              payload:res.data
            })
        })
    }
  },
  modules: {
  }
})
